#nullable enable

#if __IOS__
using System;
using System.Collections.Generic;
using CoreAnimation;
using Foundation;
using UIKit;
using Uno.Extensions;
using Uno.Logging;

namespace Windows.UI.Composition
{
	public partial class SpriteVisual : ContainerVisual
	{
		partial void OnBrushChangedPartial(CompositionBrush? brush)
		{
			if (brush is CompositionColorBrush b)
			{
				NativeLayer.BackgroundColor = b.Color;
			}
			else
			{
				this.Log().Error($"The brush type {brush?.GetType()} is not supported for sprite visuals.");
			}
		}

		internal override void StartAnimationCore(string propertyName, CompositionAnimation animation)
		{
			base.StartAnimationCore(propertyName, animation);

			switch (animation)
			{
				case KeyFrameAnimation kfa:
					AnimateKeyFrameAnimation(propertyName, kfa);
					break;
			}
		}

		private void AnimateKeyFrameAnimation(string propertyName, KeyFrameAnimation kfa)
		{
			switch(propertyName)
			{
				case nameof(RotationAngleInDegrees):
					var animations = new List<UnoCoreAnimation>();
					for (int i = 0; i < kfa.Keys.Length-1; i++)
					{
						animations.Add(CreateCoreAnimation(NativeLayer, kfa.Keys[i], kfa.Keys[i+1], "transform.rotation", value => new NSNumber(ToRadians(value))));
					}

					foreach(var a in animations)
					{
						a.Start();
					}
					break;
			}
		}

		private UnoCoreAnimation CreateCoreAnimation(
			CALayer layer, 
			KeyFrameAnimation.KeyFrame from,
			KeyFrameAnimation.KeyFrame to, 
			string property, 
			Func<float, NSValue> nsValueConversion
		)
		{
			var timingFunction = CAMediaTimingFunction.FromName(CAMediaTimingFunction.Linear);

			return new UnoCoreAnimation(
				layer, 
				property,
				from.Value,
				to.Value,
				from.NormalizedProgressKey,
				from.NormalizedProgressKey - to.NormalizedProgressKey, 
				timingFunction,
				nsValueConversion,
				FinalizeAnimation,
				false
			);
		}

		private void FinalizeAnimation() { }
	}
}
#endif
